package com.katze.boot.file;

import com.katze.common.env.AppEnvironment;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public enum DataSourceEnum {

    ASN1("20010", "ASN.1上传", "General/Asn1"),
    CODE_GENERATOR("20014", "代码生成器--代码模板上传", ""),
    LOGGER_COLLECT("20016", "日志采集--日志文件上传", ""),
    HADOOP("20018", "Hadoop上传", ""),
    WORKSPACE("10050", "个人工作空间", ""),
    EXCEL("20020", "应用助手--Excel导入", "General/Excel");

    private final Map<String, Path> cache = new ConcurrentHashMap<>();

    private final String code;

    private final String description;

    private final String directory;

    DataSourceEnum(String code, String description, String directory) {
        this.code = code;
        this.description = description;
        this.directory = directory;
    }

    public String code() {
        return code;
    }

    public String description() {
        return description;
    }

    public Path toPath(String... name) {
        Path dir = getPath(directory);
        if (name.length > 0) {
            for (String item : name) {
                dir = dir.resolve(item);
            }
            if (Files.notExists(dir)) {
                dir.toFile().mkdirs();
            }
        }
        return dir;
    }

    private Path getPath(String directory) {
        if (directory == null) {
            return cache.computeIfAbsent("FileStorage", (k) -> {
                File temp = new File(AppEnvironment.DATA_DIR.getValue(), k);
                if (!temp.exists()) temp.mkdir();
                return temp.toPath();
            });
        } else {
            return cache.computeIfAbsent(directory, (k) -> {
                Path temp = getPath(null).resolve(k);
                if (Files.notExists(temp)) temp.toFile().mkdirs();
                return temp;
            });
        }
    }
}
